package com.qulei.demo.db.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.qulei.demo.DBApplication;
import com.qulei.demo.db.builder.DBBuilder;
import com.qulei.demo.db.builder.DBBuilderImpl;
import com.qulei.demo.db.utils.DBHelper;
import com.qulei.demo.db.utils.TableHelper;

public class DBBaseDaoImpl<T> implements DBBaseDao<T> {

	protected DBBuilder<T> iDataBuilder;
	protected DBHelper dbHelper;

	public DBBaseDaoImpl(Class<T> clazz) {
		this.iDataBuilder = new DBBuilderImpl<T>(clazz);
		dbHelper = DBHelper.getInstance(DBApplication.getInstance());
	}

	@Override
	public long insert(SQLiteDatabase db, T t) {

		ContentValues values = iDataBuilder.deconstruct(t,
				TableHelper.CONTENT_VALUES_CREATE);
		String table = iDataBuilder.tableName();

		if (table != null && values != null) {
			return db.insert(table, null, values);
		} else {
			return -1l;
		}

	}

	@Override
	public int update(SQLiteDatabase db, T t, String whereClause,
			String[] whereArgs) {

		ContentValues values = iDataBuilder.deconstruct(t,
				TableHelper.CONTENT_VALUES_UPDATE);
		String table = iDataBuilder.tableName();

		if (table != null && values != null) {
			return db.update(table, values, whereClause, whereArgs);
		} else {
			return -1;
		}

	}

	@Override
	public int delete(SQLiteDatabase db, String whereClause, String[] whereArgs) {
		return db.delete(iDataBuilder.tableName(), whereClause, whereArgs);
	}

	@Override
	public List<T> select(SQLiteDatabase db, String selection,
			String[] selectionArgs, String groupBy, String having,
			String orderBy, String limit) {

		List<T> list = null;
		Cursor cursor = db.query(iDataBuilder.tableName(), null, selection,
				selectionArgs, groupBy, having, orderBy, limit);

		if (cursor != null && cursor.getCount() > 0) {
			list = new ArrayList<T>();
			if (cursor.moveToFirst()) {
				do {
					list.add(iDataBuilder.build(cursor));
				} while (cursor.moveToNext());
			}
		}
		closeCursor(cursor);
		return list;
	}

	@Override
	public List<T> selectBySql(SQLiteDatabase db, String sql,
			String[] selectionArgs) {
		List<T> list = null;
		Cursor cursor = db.rawQuery(sql, selectionArgs);
		if (cursor != null && cursor.getCount() > 0) {
			list = new ArrayList<T>();
			if (cursor.moveToFirst()) {
				do {
					list.add(iDataBuilder.build(cursor));
				} while (cursor.moveToNext());
			}
		}
		closeCursor(cursor);
		return list;
	}

	@Override
	public void closeCursor(Cursor cursor) {
		if (cursor != null) {
			cursor.close();
		}
	}

	@Override
	public void onCreateDataBase(SQLiteDatabase db) {
		String table = iDataBuilder.createTableName();
		if (table != null)
			db.execSQL(table);
	}

	@Override
	public void ononUpgradeDataBase(SQLiteDatabase db, int oldVersion,
			int newVersion) {

		// String dropTable = iDataBuilder.dropTableName();
		// if (dropTable != null)
		// db.execSQL(dropTable);
		// String createTable = iDataBuilder.createTableName();
		// if (createTable != null)
		// db.execSQL(createTable);

	}

}
